package com.btex.app.adapter;

import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.TextView;

import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.entity.MakeListInfo;
import com.btex.app.tools.SharedPreferencesUtil;
import com.btex.app.tools.Tools;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/5/18
 * @time 下午3:29
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class EthFragmentAdapter extends BaseQuickAdapter<MakeListInfo.DataBean.ETHBean, BaseViewHolder> {

    private String mS;

    public EthFragmentAdapter(@Nullable List<MakeListInfo.DataBean.ETHBean> data, String s) {
        super(R.layout.item_first_fragmnet, data);
        mS = s;
    }

    @Override
    protected void convert(BaseViewHolder helper, MakeListInfo.DataBean.ETHBean item) {


        String amount_float_num = item.getAmount_float_num();
        String price_float_num = item.getPrice_float_num();
        int i = Integer.parseInt(amount_float_num);
        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(MyApplication.getContextObject());
        boolean isEngilsh = sharedPreferences.getBoolean("isEngilsh", false);
        if (isEngilsh) {
            helper.setText(R.id.item_trade_money, "$" + item.getTicker().getRate_price_u());
//            helper.setText(R.id.item_trade_money, "$" + Tools.round(Double.valueOf(item.getTicker().getRate_price_u()),Integer.valueOf(amount_float_num)).toString());

        } else {
            helper.setText(R.id.item_trade_money, "￥" + item.getTicker().getRate_price_c());
//            helper.setText(R.id.item_trade_money, "￥" + Tools.round(Double.valueOf(item.getTicker().getRate_price_c()),Integer.valueOf(amount_float_num)).toString());
        }


//        helper.addOnClickListener(R.id.fragment_first_imag_type);
        helper.addOnClickListener(R.id.fragment_first_lin_type);
        helper.setText(R.id.item_trade_price, Tools.round(Double.valueOf(item.getTicker().getTrade_price()), Integer.valueOf(price_float_num)).toPlainString());
        helper.setText(R.id.item_trade_type, item.getType());

        String change_24h = item.getTicker().getChange_24h();
        if (change_24h.contains(",")) {
            String replace = change_24h.replace(",", "");
            change_24h = replace;
        }


        boolean ischengjiao = sharedPreferences.getBoolean("ischengjiao", false);

        if (ischengjiao) {

            helper.setBackgroundRes(R.id.item_trade_zhang_die, R.color.white);
            int length = item.getTicker().getAmount().length();
            if (length > 11) {

                String amount = item.getTicker().getAmount().substring(0, 11);

                if (amount.contains(".")) {
                    String[] split = amount.split("\\.");
                    String s = split[0];
                    if (s.length() > 10) {
                        helper.setText(R.id.item_trade_zhang_die, s);
                    } else {
                        helper.setText(R.id.item_trade_zhang_die, amount);
                    }
                } else {
                    helper.setText(R.id.item_trade_zhang_die, amount);
                }


//                String substring = item.getTicker().getAmount().substring(0, 11);
//                if (substring.contains(".")) {
//                    String replace = substring.replace(".", "");
//                    helper.setText(R.id.item_trade_zhang_die, replace);
//
//                } else {
//                    helper.setText(R.id.item_trade_zhang_die, substring);
//                }


            } else {
                helper.setText(R.id.item_trade_zhang_die, item.getTicker().getAmount().substring(0, length));
            }

            helper.setTextColor(R.id.item_trade_zhang_die, MyApplication.getContextObject().getResources().getColor(R.color.tv_black_light));
            TextView textView = (TextView) helper.getView(R.id.item_trade_zhang_die);
            textView.setTypeface(Typeface.DEFAULT_BOLD);


        } else {

            if (!TextUtils.isEmpty(change_24h)) {

                Double aDouble = Double.parseDouble(change_24h);
                double v = aDouble * 100;

                helper.setTextColor(R.id.item_trade_zhang_die, MyApplication.getContextObject().getResources().getColor(R.color.white));
                TextView textView = (TextView) helper.getView(R.id.item_trade_zhang_die);
                textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                if (aDouble > 0) {

                    helper.setBackgroundRes(R.id.item_trade_zhang_die, R.drawable.item_bg_no);
                    String s = String.valueOf(v);
                    if (s.contains(".")) {

                        String[] split = s.split("\\.");
                        String s1 = split[1];
                        String s0 = split[0];


                        if (s1.length() > 2) {

                            String substring = s1.substring(0, 2);
                            String s2 = s0 + "." + substring;

                            helper.setText(R.id.item_trade_zhang_die, "+" + s2 + "%");
                        } else {
                            String s2 = s0 + "." + s1;
                            helper.setText(R.id.item_trade_zhang_die, "+" + s2 + "%");
                        }

                    } else {

                        helper.setText(R.id.item_trade_zhang_die, "+" + s + "%");
                    }

//                    int length = String.valueOf(v).length();
//                    if (length > 4) {
//                        helper.setText(R.id.item_trade_zhang_die, "+" + String.valueOf(v).substring(0, 4) + "%");
//                    } else {
//                        helper.setText(R.id.item_trade_zhang_die, "+" + String.valueOf(v) + "%");
//                    }

                } else if (aDouble == 0) {
                    String s = String.valueOf(v);
                    helper.setBackgroundRes(R.id.item_trade_zhang_die, R.drawable.item_bg_no);
                    helper.setText(R.id.item_trade_zhang_die, "+" + s + "%");
                } else {

                    helper.setBackgroundRes(R.id.item_trade_zhang_die, R.drawable.item_bg);
                    String s = String.valueOf(v);
                    if (s.contains(".")) {

                        String[] split = s.split("\\.");
                        String s1 = split[1];
                        String s0 = split[0];
                        if (s1.length() > 2) {

                            String substring = s1.substring(0, 2);
                            String s2 = s0 + "." + substring;
                            helper.setText(R.id.item_trade_zhang_die, s2 + "%");
                        } else {

                            String s2 = s0 + "." + s1;
                            helper.setText(R.id.item_trade_zhang_die, s2 + "%");
                        }

                    } else {

                        helper.setText(R.id.item_trade_zhang_die, s + "%");
                    }

//                    int length = String.valueOf(v).length();
//                    if (length > 4) {
//                        helper.setText(R.id.item_trade_zhang_die, String.valueOf(v).substring(0, 5) + "%");
//                    } else {
//                        helper.setText(R.id.item_trade_zhang_die, String.valueOf(v) + "%");
//                    }
                }
            }
        }


        int collect_id = item.getCollect_id();

        if (collect_id > 0) {
            helper.setBackgroundRes(R.id.fragment_first_imag_type, R.mipmap.xuanze);

        } else {
            helper.setBackgroundRes(R.id.fragment_first_imag_type, R.mipmap.xuan_zhe_mo);
        }


    }
}
